package com.yequan.work;

import com.alibaba.fastjson.*;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * @author yequan
 * @date 2021/02/03 17:04
 **/
public class JsonMapDemo {

    public static void main(String[] args) {
//        put();
//        getOperateType();
//        array();
//        blankList();
//        System.out.println(JsonMapper.toJson(null));
//        testMixRide();
//        forTest();
        forNull();
    }

    public static void forNull() {
        List<Coupon> couponList = null;
        System.out.println(JsonMapper.toJson(couponList));
        System.out.println(JsonMapper.json2List(null, Coupon.class));
        System.out.println(JsonMapper.json2Bean(null, Coupon.class));
        System.out.println(JsonMapper.json2Map(null));

        ReplyMap replyMap = new ReplyMap();
        replyMap.put("o", "");
        System.out.println(replyMap.success().toJson());
    }

    public static void forTest() {
        for (int i = 0; i < 10; i++) {
            if (i == 3){
                continue;
            }
            System.out.println(i);
        }
    }

    public static void testMixRide() {
        String extra = "{\"multi_price\":\"56.0\",\"available_start_time_from\":\"20210730111500\",\"multi_price_one\":\"56.0\",\"base_distance\":\"0.0\",\"multi_accept_price_one\":\"56.0\",\"available_start_time_to\":\"20210730114500\",\"transfer_money\":\"1500.0\",\"single_price\":\"87.1\",\"distance_money\":\"4100.0\",\"multi_ride_price\":\"56.0\",\"person_num_detail\":\"{\\\"adult\\\":1,\\\"child\\\":0,\\\"baby\\\":0,\\\"junior\\\":0}\",\"ride_city\":1,\"declinedId:\":833789603465920514,\"multi_save_price\":0.0,\"serve_fee_bearer\":\"0\",\"mix\":1,\"multi_accept_price\":\"56.0\",\"single_price_one\":\"87.1\"}";
        System.out.println(mixRide(extra));
    }

    public static boolean mixRide(String extra) {
        if (StringUtils.isEmpty(extra)) {
            return false;
        }
        Map<String, Object> extraMap = JsonMapper.json2Map(extra);
        Object mixObj = null;
        if (Objects.isNull(extraMap) || Objects.isNull(mixObj = extraMap.get("mix"))) {
            return false;
        }
        return (Integer) mixObj == 1;
    }

    public static void blankList() {
        List<Coupon> couponList = new ArrayList<>();
        String s = JsonMapper.toJson(couponList);
        System.out.println(s);
        List<Coupon> couponList1 = JsonMapper.json2List(s, Coupon.class);
        System.out.println(couponList1);
    }

    public static void array() {
        String[] rideIds = new String[]{};
        String s = JsonMapper.toJson(rideIds);
        String data = "";
        System.out.println(s);
        System.out.println(StringUtils.isBlank(data));
        List<String> strings = JsonMapper.json2List(s, String.class);
        System.out.println(strings);
    }

    public static void put() {
        String json = "{\"name\":\"startLon\",\"value\":\"120.739316\"}";
        Map<String, Object> map = JsonMapper.json2Map(json);
        map.put("value", "000");
        String s = JsonMapper.toJson(map);
        System.out.println(s);
    }

    public static Map<String, Object> json2Map(String jsonString) {
        Map<String, Object> map = null;
        try {
            map = JSON.parseObject(jsonString, new TypeReference<Map<String, Object>>() {
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    public static void keySet() {
        String message = "{\"user_group_user_1917\":\"1\",\"null\":\"3\",\"user_group_user_754\":\"1\",\"user_group_user_1918\":\"1\",\"user_group_user_897\":\"1\",\"user_group_user_2707\":\"1\",\"user_group_user_793\":\"1\",\"user_group_user_2846\":\"1\",\"user_group_user_2704\":\"1\",\"user_group_user_2624\":\"1\",\"user_group_user_1435\":\"1\",\"user_group_user_1336\":\"1\",\"user_group_user_2668\":\"1\",\"user_group_user_2526\":\"1\",\"user_group_user_1599\":\"1\",\"user_group_user_637\":\"1\",\"user_group_user_2662\":\"1\",\"user_group_user_1576\":\"1\",\"user_group_user_856\":\"1\",\"user_group_user_636\":\"1\",\"user_group_user_1135\":\"1\",\"user_group_user_2641\":\"1\",\"user_group_user_1462\":\"1\",\"user_group_user_494\":\"1\",\"user_group_user_424\":\"1\",\"user_group_user_663\":\"1\",\"user_group_user_2617\":\"1\",\"user_group_user_1647\":\"1\",\"user_group_user_2637\":\"1\",\"user_group_user_2437\":\"1\",\"user_group_user_2635\":\"1\",\"user_group_user_1027\":\"1\",\"user_group_user_2578\":\"1\",\"user_group_user_1469\":\"1\",\"user_group_user_1466\":\"1\",\"user_group_user_427\":\"1\",\"user_group_user_1025\":\"1\",\"user_group_user_1366\":\"1\",\"user_group_user_1487\":\"1\",\"user_group_user_1350\":\"1\",\"user_group_user_1394\":\"1\",\"user_group_user_2100\":\"1\",\"user_group_user_2661\":\"1\",\"user_group_user_2681\":\"1\",\"user_group_user_1032\":\"1\",\"user_group_user_1054_p\":\"1\",\"test_user_group_user_52\":\"1\",\"leimu_mobileno_risk_tag\":\"10\"}";
        String o = "{}";
        Map<String, Object> map = json2Map(message);
        Set<String> keySet = map.keySet();
        Iterator<String> iterator = keySet.iterator();
        StringBuilder stringBuilder = new StringBuilder();
        System.out.println("keySet size: " + keySet.size());
        while (iterator.hasNext()) {
            stringBuilder.append(iterator.next()).append("|");
        }
        System.out.println(stringBuilder.toString());
        String[] splits = stringBuilder.toString().split("\\|");
        System.out.println(splits.length);
    }

    public static void getOperateType() {
//        String extra = "{\"multi_price\":\"66.0\",\"operate_subsidy_status\":\"1\",\"operate_type\":\"2\",\"available_start_time_from\":\"20210426160000\",\"multi_price_one\":\"66.0\",\"base_distance\":\"5.0\",\"multi_accept_price_one\":\"66.0\",\"available_start_time_to\":\"20210426160000\",\"transfer_money\":\"1500.0\",\"single_price\":\"84.8\",\"distance_money\":\"5100.0\",\"multi_ride_price\":\"66.0\",\"ride_city\":0,\"operate_subsidy\":3000,\"multi_save_price\":0.0,\"serve_fee_bearer\":\"0\",\"multi_accept_price\":\"66.0\",\"single_price_one\":\"84.8\"}";
        String extra = "{\"multi_price\":\"66.0\",\"operate_subsidy_status\":\"1\",\"available_start_time_from\":\"20210426160000\",\"multi_price_one\":\"66.0\",\"base_distance\":\"5.0\",\"multi_accept_price_one\":\"66.0\",\"available_start_time_to\":\"20210426160000\",\"transfer_money\":\"1500.0\",\"single_price\":\"84.8\",\"distance_money\":\"5100.0\",\"multi_ride_price\":\"66.0\",\"ride_city\":0,\"operate_subsidy\":3000,\"multi_save_price\":0.0,\"serve_fee_bearer\":\"0\",\"multi_accept_price\":\"66.0\",\"single_price_one\":\"84.8\"}";
        String operate_type = getValueFromExtra(extra, "operate_type", String.class, "0");
        System.out.println(Integer.parseInt(operate_type));
    }

    public static <T> T getValueFromExtra(String extra, String key, Class<T> cla, T defaultValue) {
        if (StringUtils.isBlank(extra)) {
            return defaultValue;
        }
        Map<String, Object> extraMap = JsonMapper.json2Map(extra);
        if (extraMap == null) {
            return defaultValue;
        }
        Object value = extraMap.get(key);
        if (value == null) {
            return defaultValue;
        }
        return (T) value;
    }


}
